#!/usr/bin/python
# -*- coding: utf-8 -*-

import time

from jiaju.business import user_manager
from jiaju.common import exception_mgr
from jiaju.common import log_execption
from jiaju.common import JiaJuException
from django.http import HttpResponseRedirect

def ensure_success(input_function):
    """确保程序一成功执行装饰器
    @param input_function:被装饰函数 
    @return: 替换函数
    """
    def replace_function(*args, **kwargs):
        while True:
            try:
                return input_function(*args, **kwargs)
            except:
                exception_mgr.on_except()
                time.sleep(10)
                
    replace_function.func_name = input_function.func_name
    replace_function.__doc__ = input_function.__doc__
    return replace_function

def rpc_privilege_required(page_id=0, operator_id=0):
    """装饰函数，检查当前用户是否对指定功能有查询权限
    @param page_id:页面编号 
    @param operator_id:操作编号
    """
    def decorator_funtion(input_function): #定义一个新函数，接收被装饰的函数
        def replace_function(*args, **kwargs): #被替换执行的函数，主要工作就在这里完成
            request = args[0]#这里要求参数传进来，第一个必须是request对象
            privilege = user_manager.get_privilege(request, page_id)
            if not privilege or privilege.privilege_value % operator_id != 0:
                raise Exception("没有权限")
            try:
                return input_function(*args, **kwargs)
            except:
                full_path = request.get_full_path()
                exception_mgr.on_except('request url: %r' % full_path, 1)
                log_execption('request url: %r' % full_path, 1)

        replace_function.func_name = input_function.func_name
        replace_function.__module__ = input_function.__module__ 
        replace_function.__doc__ = input_function.__doc__
        return replace_function
    return decorator_funtion


def privilege_required(page_id=0, operator_id=0):
    """装饰函数，检查当前用户是否对指定功能有查询权限
    @param page_id:页面编号 
    @param operator_id:操作编号
    """
    def decorator_funtion(input_function): #定义一个新函数，接收被装饰的函数
        def replace_function(*args, **kwargs): #被替换执行的函数，主要工作就在这里完成
            request = args[0]#这里要求参数传进来，第一个必须是request对象
            privilege = user_manager.get_privilege(request, page_id)
            uservo = user_manager.get_userinfo(request)
            if uservo == None :
                reponse = HttpResponseRedirect("/admin/login/")
                return reponse
            if not privilege or privilege.privilege_value % operator_id != 0:
                response = HttpResponseRedirect("/admin/noprivilege/")
                return response
            try:
                return input_function(*args, **kwargs)
            except:
                full_path = request.get_full_path()
                exception_mgr.on_except('request url: %r' % full_path, 1)
                log_execption('request url: %r' % full_path, 1)

        replace_function.func_name = input_function.func_name
        replace_function.__module__ = input_function.__module__ 
        replace_function.__doc__ = input_function.__doc__
        return replace_function
    return decorator_funtion
